<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="staffName">
        <el-input v-model="queryParams.staffName" placeholder="请输入人员姓名" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
<el-button type="primary" plain icon="el-icon-plus" size="small" @click="handleAdd" v-hasPermi="['iot:staff:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
<el-button type="success" plain icon="el-icon-edit" size="small" :disabled="single" @click="handleUpdate" v-hasPermi="['iot:staff:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
<el-button type="danger" plain icon="el-icon-delete" size="small" :disabled="multiple" @click="handleDelete" v-hasPermi="['iot:staff:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="staffList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="人员编号" align="center" prop="staffNo" />
      <el-table-column label="人员姓名" align="center" prop="staffName" />
      <el-table-column label="技能等级" prop="skillLevel" align="center" width="120">
        <template slot-scope="scope"><dict-tag :options="dict.type.skill_level" :value="scope.row.skillLevel"/></template>
      </el-table-column>
      <el-table-column label="专业技能/专长" align="center" prop="specialties" />
      <el-table-column label="手机电话" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="工作状态" prop="workStatus" align="center" width="120">
        <template slot-scope="scope"><dict-tag :options="dict.type.work_status" :value="scope.row.workStatus"/></template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['iot:staff:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['iot:staff:remove']">删除</el-button>
            <el-button size="mini" type="text" icon="el-icon-message" @click="operateUp(scope.row)" v-hasPermi="['iot:staff:listt']">操作记录</el-button>
          </template>
        </el-table-column></el-table>
<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" :page-sizes="pageSizes" @pagination="getList"/>
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="102px">
        <el-form-item label="人员编号" prop="staffNo">
          <el-input v-model="form.staffNo" placeholder="请输入人员编号" />
        </el-form-item>
        <el-form-item label="人员姓名" prop="staffName">
          <el-input v-model="form.staffName" placeholder="请输入人员姓名" />
        </el-form-item>
        <el-form-item label="技能等级" prop="skillLevel">
          <el-select v-model="form.skillLevel" placeholder="请选择技能等级" filterable style="width:100%">
            <el-option v-for="dict in dict.type.skill_level" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="专业技能/专长" prop="specialties">
          <el-input v-model="form.specialties" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="工作状态" prop="workStatus">
          <el-select v-model="form.workStatus" placeholder="请选择工作状态" filterable style="width:100%">
            <el-option v-for="dict in dict.type.work_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="手机电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机电话" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item></el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button><el-button @click="cancel">取 消</el-button>
      </div></el-dialog></div></template>
<script>
import { listStaff, getStaff, delStaff, addStaff, updateStaff } from "@/api/iot/staff";
export default {
  dicts:['skill_level','work_status'],
  name: "Staff",
  data() {
    return {
      pageSizes: [10,20,30,50],
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维保人员表格数据
      staffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        adminId: null,
        customerId: null,
        projectId: null,
        staffNo: null,
        staffName: null,
        skillLevel: null,
        specialties: null,
        phone: null,
        email: null,
        workStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        staffNo: [
          { required: true, message: "人员编号不能为空", trigger: "blur" }
        ],
        staffName: [
          { required: true, message: "人员姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号码", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询维保人员列表 */
    getList() {
      this.loading = true;
      listStaff(this.queryParams).then(response => {
        this.staffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        adminId: null,
        customerId: null,
        projectId: null,
        staffNo: null,
        staffName: null,
        skillLevel: null,
        specialties: null,
        phone: null,
        email: null,
        workStatus: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStaff(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStaff(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStaff(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除ID为"' + ids + '"的数据项？').then(function() {
        return delStaff(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('iot/staff/export', {
        ...this.queryParams
      }, `staff_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
